package cn.aishua.data;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import cn.aishua.util.FormatConverter;

public class FindDataParser
{

    public ArrayList<FindVendor> data = new ArrayList<FindVendor>();
    public int count = 0;

    // public ArrayList<FindVendor> partialData = new ArrayList<FindVendor>();
    // public int partialCount = 0;
    int total;

    //	int current = 0;
    //	int total;

    public FindDataParser()
    {
        //		if(raw.length() != 0)
        //			parse(raw);
    }

    /**
     * 
     * @param r
     * @return how many items are there in json store
     */
    private void parse( String r )
    {
        try
        {
            JSONObject obj = new JSONObject( r );
            JSONArray a = obj.getJSONArray( "store" );
            total = obj.getInt( "total" );

            for( int i = 0; i < a.length(); i++ )
            {
                JSONObject temp = a.getJSONObject( i );
                data.add( makeVendor( temp ) );
                count++;
            }
        }
        catch( JSONException e )
        {
            Log.d( "FindDataParser", e.getMessage() );
        }
    }

    /**
     * 
     * @param newInfo
     * @return 0 - no more items to load, 1 - still more items
     */
    public int refresh( String newInfo )
    {

        //		int preCount = count;
        parse( newInfo );
        Log.d( "FindDataParser", "count: " + count );
        Log.d( "FindDataParser", "total: " + total );

        //FIXME
        return count < total ? 1 : 0;
    }

    private FindVendor makeVendor( JSONObject temp )
    {
        FindVendor obj = new FindVendor();

        try
        {
            obj.id = temp.getInt( "id" );
            Log.d( "test", "id " + obj.id );
            obj.name = temp.getString( "name" );
            obj.average_price = (float) temp.getDouble( "average_price" );
            obj.max_customer_rate = (float) temp.getDouble( "max_customer_rate" );
            obj.file_url = temp.getString( "file_url" );

            obj.distance = Integer.valueOf( FormatConverter.toIntFormat( temp.getString( "distance" ) ) );
            Log.d( "test", "distance " + obj.distance );
            JSONArray a = temp.getJSONArray( "merchant_tags" );
            for( int i = 0; i < a.length(); i++ )
            {
                JSONObject tag = a.getJSONObject( i );
                
                // only need tag whose tag type is business
                String tagType = tag.getString( "tag_type" );
                if("business".equals( tagType ))
                {
                    obj.merchant_tags.add( tag.getString( "name" ) );
                }
            }

            obj.business_circle = temp.getJSONObject( "business_circle" ).getString( "name" );

            JSONArray b = temp.getJSONArray( "sub_category" );
            for( int i = 0; i < b.length(); i++ )
            {
                obj.sub_category.add( b.getJSONObject( i ).getString( "name" ) );
            }

            JSONArray pareCateArray = temp.getJSONArray( "parent_category" );
            if( pareCateArray != null && pareCateArray.length() > 0 )
            {
                obj.pare_category = pareCateArray.getJSONObject( 0 ).getString( "name" );
            }

            obj.lati = temp.getString( "gps_x" );
            obj.longt = temp.getString( "gpx_y" );

        }
        catch( JSONException e )
        {
            Log.d( "FindDataParser", e.getMessage() );
        }
        return obj;
    }

    public static class FindVendor
    {
        public int id;
        public String name;
        public float average_price;
        public float max_customer_rate;
        public String file_url;
        public int distance;
        public ArrayList<String> merchant_tags = new ArrayList<String>();
        public String business_circle;
        public ArrayList<String> sub_category = new ArrayList<String>();
        public String pare_category;
        public String lati;
        public String longt;
    }

}
